//热门歌单
function setTop4hot() {
	var hot4setId = $(".list_mask");
	var hot4corver = $("#hot_music > li > a > img");
	var hot4describe = $("#hot_music div[class='descript'] div[class='list_descript']");
	var hot4creator = $("#hot_music div[class='creator']");
	$.ajax({
		"url" : "http://192.168.11.240:3000/top/playlist",
		"type" : "get",
		"data" : {
			"order" : "hot"
		},
		"success" : function(data) {
			var playlists = data.playlists;
			for (var i = 0; i < 12; i++) {
				$(hot4corver[i]).prop("src",
						playlists[i].coverImgUrl + "?param=230y230");
				$(hot4setId[i]).prop("id", playlists[i].id);
				$(hot4describe[i]).attr("style", "font-size:14px");
				$(hot4describe[i]).text("[歌单简介]:" + playlists[i].description);
				$(hot4creator[i]).text(
						"[歌单作者]:" + playlists[i].creator.nickname);
			}
		}
	});
}

// 设置歌曲url
function setMusicUrl(i, MusicId) {
	var top4url = $("#top_music > li > a");
	$.ajax({
		"url" : "http://192.168.11.240:3000/song/url",
		"type" : "get",
		"data" : {
			"id" : MusicId
		},
		"success" : function(data) {
			var songInfo = data.data[0];
			url = songInfo.url;
			$(top4url[i]).attr("href", url);
		}
	});
}

// 获取歌曲url
function getMusicUrl(MusicId) {
	var url = null;
	$.ajax({
		"async" : false,
		"url" : "http://192.168.11.240:3000/song/url",
		"type" : "get",
		"data" : {
			"id" : MusicId
		},
		"success" : function(data) {
			var songInfo = data.data[0];
			url = songInfo.url;
		}
	});
	return url;
}

// 新歌首发展示
function setTop4new() {
	var top4name = $("#top_music .info .title > a");
	var top4corver = $("#top_music > li > a > img");
	var top4id = $("#top_music > li > a");
	var top4author = $("#top_music .info > #author");
	// 新歌首发
	$.ajax({
		"url" : "http://192.168.11.240:3000/top/list?",
		"type" : "get",
		"data" : {
			"idx" : "0"
		},
		"success" : function(data) {
			var tracks = data.playlist.tracks;
			for (var i = 0; i < 4; i++) {
				var obj = tracks[i];
				// setMusicUrl(i, obj.id);不能将歌曲url直接放在a标签
				$(top4name[i]).text(obj.name);
				$(top4corver[i]).prop("src", obj.al.picUrl + "?param=230y230");
				$(top4id[i]).attr("id", obj.id);
				$(top4author[i]).text(obj.ar[0].name);
			}
		}
	});

}

// 排行榜
function setTop(topTypeId, selector, maskIndex) {
	var targetLi = $(".rank-list li")[maskIndex];
	$(targetLi).attr("id", topTypeId);
	var targetDiv = $(".rank-list .title .sandy")[maskIndex];
	$(targetDiv).attr("id", topTypeId);
	$
			.ajax({
				"url" : "http://192.168.11.240:3000/top/list?",
				"type" : "get",
				"data" : {
					"idx" : topTypeId
				},
				"success" : function(data) {
					var tracks = data.playlist.tracks;
					for (var i = 0; i < 4; i++) {
						var obj = tracks[i];
						var top4new = selector;
						top4new
								.append("<br/><ul><li style='text-align:left'><a href='javascript:void(0)' class='playit' id="
										+ obj.id
										+ ">"
										+ obj.name
										+ "<br/>- "
										+ obj.ar[0].name + " </a></ul><br/>");
					}
				}
			});
}

// 获取歌曲信息
function getMusicInfo(MusicId) {
	var picUrl = null;
	$.ajax({
		"async" : false,
		"url" : "http://192.168.11.240:3000/song/detail",
		"type" : "get",
		"data" : {
			"ids" : MusicId
		},
		"success" : function(data) {
			picUrl = data.songs[0].al.picUrl;
		}
	});
	return picUrl;
}

// 获取歌曲名
function getMusicName(MusicId) {
	var songName = null;
	$.ajax({
		"async" : false,
		"url" : "http://192.168.11.240:3000/song/detail",
		"type" : "get",
		"data" : {
			"ids" : MusicId
		},
		"success" : function(data) {
			songName = data.songs[0].name;
		}
	});
	return songName;
}

// 播放音乐
$(document)
		.on(
				'click',
				'#like_layer .btn-default',
				function() {
					var picUrl = $("#like_layer .btn-default").attr("style");
					if (picUrl == "background-image:url('images/dislike.png')") {
						$
								.ajax({
									"url" : "../ms.do?ac=like",
									"data" : {
										"op" : "like",
										"id" : $(this).attr("id"),
										"name" : getMusicName($(this)
												.attr("id"))
									},
									"success" : function(data) {
										if (data.isOk == true) {
											$("#like_layer .btn-default")
													.attr("style",
															"background-image:url('images/like.png')");
										}
									}
								});
					} else {
						$("#like_layer .btn-default").attr("style",
								"background-image:url('images/dislike.png')");
						$
								.ajax({
									"url" : "../ms.do?ac=like",
									"data" : {
										"op" : "dislike",
										"id" : $(this).attr("id"),
										"name" : getMusicName($(this)
												.attr("id"))
									},
									"success" : function(data) {
										if (data.isOk == true) {
											$("#like_layer .btn-default")
													.attr("style",
															"background-image:url('images/dislike.png')");
										}
									}
								});
					}

				});

// 所有playit类的点击事件
$(document).on(
		'click',
		'.playit',
		function() {
			// 获取到音乐id
			var musicID = $(this).attr("id");
			var url = getMusicUrl(musicID);
			var purl = getMusicInfo(musicID);
			if (url!=null && url != "") {
				// 播放音乐
				$(".modal-footer audio").attr("id", musicID);
				$(".modal-footer audio").attr("src", url);				
				$("#like_layer .btn-default").attr("id", musicID);
				$(".modal-footer audio").attr("src", url);
				$("#like_layer button").attr("id", musicID);
				$("#playdock").css("background",
						"url(" + purl + "?param=100y100" + ")");
				$("#mcorver").css(
						{
							"background-image" : "url(" + purl
									+ "?param=700y700" + ")",
							"background-position" : "center",
							// "background-repeat":"no-repeat",
							"background-size" : "700px 700px",
							"height" : "700px"
						});
				$("#playlist").prepend(
						"<li><a class='button playit' id=" + musicID
								+ "></a><li>");
				
				
			} else {
				// lightyear.loading('show');
				// 假设ajax提交操作
				setTimeout(function() {
					// lightyear.loading('hide');
					lightyear.notify('由于版权因素,目前无法播放~', 'danger');
				}, 1e3);
			}
		});
$(document).on("click", ".sandy", function() {
	var scode = $(this).attr("id");
	window.open("playList.jsp?type=top&scode=" + scode);
});

// 歌单事件
$(document).on("click", ".list_mask", function() {
	var id = $(this).attr("id");
	window.open("playList.jsp?type=ulist&scode=" + id);
});

// 获取歌曲信息
function getSongDetailInfo(MusicId) {
	var song = null;
	$.ajax({
		"async" : false,
		"url" : "http://192.168.11.240:3000/song/detail",
		"data" : {
			"ids" : MusicId
		},
		"success" : function(data) {
			song = data;
		}
	});
	return song;
};

// 获取歌曲信息
function getSongsDetailInfo(MusicIds) {
	var song = null;
	var ids = "";
	var lth = MusicIds.length - 1;
	for (var i = 0; i <= lth; i++) {
		if (i != lth) {
			ids += MusicIds[i].id + ",";
		} else {
			ids += MusicIds[i].id;
		}
	}
	$.ajax({
		"async" : false,
		"url" : "http://192.168.11.240:3000/song/detail",
		"data" : {
			"ids" : ids
		},
		"success" : function(data) {
			song = data;
		}
	});
	return song;
};
